import mock from "mockjs";

// 生成角色列表数据
let roleList = mock.mock({
  "list|5-10": [
    {
      "id|+1": 1,
      "name": "@cword(2,5)", // 2-5个中文字符的角色名称
      "description": "@cparagraph(1,3)", // 1-3句话的角色描述
      "createTime": "@date",
      "permissions|1-10": [
        {
          "id|+1": 100,
          "name": "@cword(2,8)"
        }
      ]
    }
  ]
});

// 生成用户列表数据
let userList = mock.mock({
  "list|15-30": [
    {
      "id|+1": 1001,
      "username": "@name",
      "nickname": "@cname",
      "email": "@email",
      "phone": "@integer(13000000000,13999999999)",
      "roleId|1-10": 1,
      "status|1": [0, 1], // 0表示禁用，1表示启用
      "createTime": "@date"
    }
  ]
});

/**
 * 获取角色列表
 * @param {Object} req - 请求对象
 * @returns {Object} - 角色列表数据
 */
export function roleListMock(req) {
  return {
    code: 200,
    msg: "获取角色列表成功",
    data: {
      list: roleList.list,
      count: roleList.list.length
    }
  };
}

/**
 * 获取用户列表（预留）
 * @param {Object} req - 请求对象
 * @returns {Object} - 用户列表数据
 */
export function userListMock(req) {
  const params = JSON.parse(req.body);
  const { pageNum = 1, pageSize = 10, username = "", status } = params;
  
  // 过滤数据
  let filteredList = [...userList.list];
  if (username) {
    filteredList = filteredList.filter(item => 
      item.username.includes(username) || item.nickname.includes(username)
    );
  }
  if (status !== undefined) {
    filteredList = filteredList.filter(item => item.status === status);
  }
  
  // 计算总数
  const total = filteredList.length;
  
  // 计算分页
  const startIndex = (pageNum - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  const paginatedList = filteredList.slice(startIndex, endIndex);
  
  return {
    code: 200,
    msg: "获取用户列表成功",
    data: {
      list: paginatedList,
      count: total,
      pageNum: parseInt(pageNum),
      pageSize: parseInt(pageSize)
    }
  };
}